package com.crm.business.service.impl;


import com.alibaba.fastjson2.filter.BeanContext;
import com.crm.business.domain.TbBusiness;
import com.crm.business.domain.TbBusinessRecord;
import com.crm.business.domain.dto.TbBursinessDto;
import com.crm.business.domain.vo.TbBusinessVo;
import com.crm.business.mapper.BusinessMapper;
import com.crm.business.service.BusinessService;
import com.crm.common.core.domain.entity.SysUser;
import com.crm.common.domain.vo.AssignMentVo;
import com.crm.common.utils.DateUtils;
import com.crm.common.utils.SecurityUtils;
import com.crm.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ISysUserService sysUserService;

    private static final Integer MO_FA_COUNT = 1;

    /**
     * 商机分页查询
     *
     * @param tbBusinessVo
     * @return
     */
    @Override
    public List<TbBusinessVo> selectBusinessList(TbBusinessVo tbBusinessVo) {
        return businessMapper.selecBusinessList(tbBusinessVo);
    }

    /**
     * 公海池分页查询
     *
     * @param tbBusiness
     * @return
     */
    @Override
    public List<TbBusiness> selectBusinessPool(TbBusiness tbBusiness) {
        return businessMapper.selecBusinessPool(tbBusiness);
    }

    /**
     * 商机跟进记录查询
     *
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessRecord> selectRecordList(Long id) {
        return businessMapper.selectRecordList(id);
    }

    /**
     * 商机分配
     *
     * @param assignMentVo
     */
    @Override
    public void assignment(AssignMentVo assignMentVo) {
        List<Long> ids = assignMentVo.getIds();
        for (Long id : ids) {
            TbBusiness business = businessMapper.selectBusinessById(id);
            SysUser sysUser = sysUserService.selectUserById(assignMentVo.getUserId());
            business.setOwner(sysUser.getUserName());
            business.setUpdateBy(SecurityUtils.getUsername());
            business.setOwnerTime(DateUtils.getNowDate());
            business.setStatus("2");
            businessMapper.updateById(business);
        }
    }

    /**
     * 根据id查询商机详情
     *
     * @param id
     * @return
     */
    @Override
    public TbBusiness getById(Long id) {
        return businessMapper.selectBusinessById(id);
    }

    /**
     * 商机新增
     *
     * @param tbBusiness
     * @return
     */
    @Override
    public void add(TbBusiness tbBusiness) {
        tbBusiness.setCreateBy(SecurityUtils.getUsername());
        tbBusiness.setCreateTime(DateUtils.getNowDate());
        tbBusiness.setOwner(SecurityUtils.getUsername());
        businessMapper.insertTbBusiness(tbBusiness);
    }

    /**
     * 商机跟进记录新增
     *
     * @param tbBursinessDto
     */
    @Override
    @Transactional
    public int addRecord(TbBursinessDto tbBursinessDto) {
        // 将获取到的一坨,拷贝到商机表 并修改
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(tbBursinessDto, tbBusiness);
        tbBusiness.setStatus("2");
        businessMapper.updateBusiness(tbBusiness);

        // 将获取到的一坨,拷贝到商机跟进记录表
        TbBusinessRecord tbBusinessRecord = new TbBusinessRecord();
        BeanUtils.copyProperties(tbBursinessDto, tbBusinessRecord);
        // 设置id为null 和 创建人 创建时间
        tbBusinessRecord.setId(null);
        tbBusinessRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessRecord.setCreateTime(DateUtils.getNowDate());
        businessMapper.insertRecord(tbBusinessRecord);
        return MO_FA_COUNT;
    }

    /**
     * 商机踢回公海池
     *
     * @param id
     * @param reasons
     * @return
     */
    @Override
    public int updateBusinessRecord(Long id, String reasons) {

        TbBusiness tbBusiness = businessMapper.selectBusinessById(id);
        if (tbBusiness == null) {
            throw new RuntimeException("该id查询到的客户信息为空: " + id);
        }
        //踢回公海池 ,修改状态 修改创建人,创建时间 为null
        tbBusiness.setOwnerTime(null);
        tbBusiness.setStatus("3");
        // 清理 跟进信息
        tbBusiness.setNextTime(null);
        tbBusiness.setUpdateTime(null);

        // 获取当前的被踢次数，并递增
        Integer currentFalseCount = tbBusiness.getFalseCount() != null ? tbBusiness.getFalseCount() : 0;
        tbBusiness.setFalseCount(currentFalseCount + 1); // 递增被踢次数

        if (currentFalseCount >= 3) {
            // 如果被踢次数大于等于3次，则将客户状态设置为"4"
            tbBusiness.setStatus("4");
        }
        // 删除跟进记录
        businessMapper.deleteRecord(id);
        return businessMapper.updateBusinessRecord(tbBusiness);
    }

    /**
     * 从公海池捞取
     *
     * @param assignMentVo
     * @return
     */
    @Override
    public int updateAgin(AssignMentVo assignMentVo) {
        // 获取要捞取的ids 遍历ids
        assignMentVo.getIds().forEach(id -> {
            TbBusiness tbBusiness = businessMapper.selectBusinessById(id);
            // 将捞取人 捞取时间 状态 赋值 捞取人就是当前登录的人
            tbBusiness.setOwner(SecurityUtils.getUsername());
            tbBusiness.setOwnerTime(DateUtils.getNowDate());
            tbBusiness.setStatus("1");
            // 更新客户信息  捞取出来
            businessMapper.updateBusiness(tbBusiness);
        });
        return MO_FA_COUNT;
    }
}
